
<div id="menu_tg">
	<div id="toolbars-rm" style="display:none;">
		<form id="searchForm">
			<div class="datagrid-toolbars">
				<div class="toolbar-row">
					<div class="group">
						<a onclick="selectAll();" href="javascript:;" class="bto"><i class="fa fa-plus bto icon-green"></i>全选</a>
						<a onclick="unselectAll();" href="javascript:;" class="bto"><i class="fa fa-pencil bto icon-green1"></i>不选</a>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>



<script>
	var $menu_tg;
	$(function(){
		
		$menu_tg = $("#menu_tg").treegrid({
			method: "GET", url: "${ctx}/manager/system/menu/treegrid",
            idField: "id", treeField: "text", fit: true, checkOnSelect: true,
            border: false, striped:true, singleSelect: false, rownumbers: true, 
            toolbar: "#toolbars-rm", sortName: 'sorting', sortOrder: 'asc', queryParams: {},
            frozenColumns: [[
            	{ field: "ck", checkbox: true },
                { field: "text", title: "名称", width: 180 }
            ]],
            columns: [[
                { field: "selected", title: "", width: 100, align: "center", formatter:function(value, row, index){
                	if(row.category == "page") {
                		return "<span class='unselectAll' check_div='check-"+row.id+"'>不选</span>/<span class='selectAll' check_div='check-"+row.id+"'>全选</span>" ;
                	}
                }},
                { field: "permits", title: "按钮权限", formatter:function(value, row, index){
                	if(undefined != value && value.length > 0){
	            		var op = "<div id='icheck' class='permit_check' check_div='check-"+row.id+"'>" ;
		            	$.each(value, function(i, p){
		            		op += "<input id='"+row.id+"-"+p.id+"' permit_id='"+p.id+"' type='checkbox' class='check-"+row.id+"'>";
		            		op += "<div class='opts'><label for='"+row.id+"-"+p.id+"'>"+p.fullName+"</label></div>";
		            	});
		            	op + "</div>" ;
		            	return op;
	            	}
	            	return "";
                }}
            ]],
            onLoadSuccess: function(){
            	$.fn.treegrid.extensions.onLoadSuccess.apply(this, arguments);
            	$menu_tg.treegrid('unselectAll');$menu_tg.treegrid('clearSelections');
            	$("#icheck input").iCheck({
	    			checkboxClass: "icheckbox_minimal-orange"
	    	   	});
	    	   	$(".unselectAll").on("click", function(){
	    	   		$("#icheck[check_div="+$(this).attr("check_div")+"]").find("input[type=checkbox]").iCheck('uncheck');
	    	   	});
	    	   	$(".selectAll").on("click", function(){
	    	   		$("#icheck[check_div="+$(this).attr("check_div")+"]").find("input[type=checkbox]").iCheck('check');
	    	   	});
	    	   	
	    	   	//加载该角色已有的权限
				$.post("${ctx}/manager/base/role/findRoleMenuByRoleId/${roleId}", function(result) {
					$.each(result, function(i, p){
						$menu_tg.treegrid("select", p.menu_id) ;
						
						var check_div = $("#icheck[check_div=check-"+p.menu_id+"]");
						if(p.permit_ids != null && p.permit_ids != "") {
							var pids = p.permit_ids.split(",") ;
							for(var i=0; i<pids.length; i++) {
								check_div.find("#"+p.menu_id+"-"+pids[i]).iCheck("check");
							}
						}
						
					});
				}, 'json').error(function() { $.easyui.loaded(); });
            },
            enableHeaderClickMenu: false, enableHeaderContextMenu: false, enableRowContextMenu: false
        });

	});
	
	function selectAll() {
		$menu_tg.treegrid("selectAll") ;
		$("#icheck[class=permit_check]").find("input[type=checkbox]").iCheck("check");
	}
	function unselectAll() {
		$menu_tg.treegrid("unselectAll") ;
		$("#icheck[class=permit_check]").find("input[type=checkbox]").iCheck("uncheck");
	}
	
	
	//提交表单数据
	var submitNow = function($d, $dg, flag) {
		var tgRows = $menu_tg.treegrid("getChecked");
	
		var menu_ids = [] ;
		var permit_ids = [];
		if(tgRows.length > 0) {
			$.each(tgRows, function(i, p){
				if(p.permits != null && p.permits.length > 0) {
					var permitAttrIds = [];
					var ps = $(".check-"+p.id+":checked");
					
					$.each(ps, function(i, g){
						permitAttrIds.push($(this).attr("permit_id"));
					});
					menu_ids.push(p.id) ;
					permit_ids.push(permitAttrIds.join(",")) ;
				}
			});
		}
		
		var data = {"role_id": "${roleId!}", "menu_ids": menu_ids.join(","), "permit_ids": permit_ids.join("|")};
		
		$.post("${ctx}/manager/base/role/addPermitToRole", data, function(result) {
			if (result.status) {
				alertify.success(result.message);$.easyui.loaded();
				if(flag==undefined||flag==false){$dg.datagrid('reload');$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.message, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	};
	
	//验证表单
	var submitForm = function($d, $dg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $dg, flag) ;
		}
	};
</script>

